package com.itheima.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;

import java.io.ByteArrayInputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * []
 *
 * @author : [itnan]
 * @version : [v1.0]
 * @createTime : [2025/2/10 20:46]
 */

public class AliOssUtils {


    private static String endpoint = "oss-cn-beijing.aliyuncs.com";
    private static String accessKeyId = "LTAI5tNXXN6ixPgW8RLVMS68";
    private static String accessKeySecret = "FrQPaT6y6DBf2CzCqoIQpwqMqmIBJ7";
    private static String bucketName = "mhr-health";

    /**
     * 文件上传
     *
     * @param bytes
     * @param objectName
     * @return
     */
    public static String upload(byte[] bytes, String objectName) {

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder
                .append("health-images")
                .append("/")
                .append(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd")) + "/")
                .append(objectName);

        try {
            // 创建PutObject请求。
            ossClient.putObject(bucketName, stringBuilder.toString(), new ByteArrayInputStream(bytes));
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        //文件访问路径规则 https://BucketName.Endpoint/health-images/2025/02/10/fileName.jpg

        return stringBuilder.toString();
    }

    /**
     * 文件删除
     *
     * @param objectName
     */
    public static void delete(String objectName) {

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // 删除文件。
            ossClient.deleteObject(bucketName, objectName);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
        }
    }
}
